define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    
    // require('globaljs');
    $.widget('rk.groupCreateNewCtrl', $.rk.oaDialogCtrl, {
        _create:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var temp = require("oa/tmpl/group/content/tpl_group_create.tpl");
            elem.html(rk.templateText(temp));
            me._listeners();
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var showDiv = elem.find("[act='group_icon_div']");
            var newSrc = elem.find("[act='group_icon_url']");
            var $picUploadBtn = $("#group_icon_sel_file").uploadPic({
                uploadify_setting: {
                    uploader: '/json/global_uploadify/img-upload.action',
                    buttonText: rk.i18n('GROUP_ICON_SELECT'),
                    buttonClass: 'uploadify-button file_select',
                    queueID: 'iconFileQueue',
                    removeCompleted: true,
                    multi:false,
                    removeTimeout: 0,
                    onUploadSuccess: function(file, data, response) {
                        var json = $.parseJSON(data);
                        if (json.status == 0) {
                            showDiv.html('<img src="'+json.data.returnParams.fileSUrl+'" alt="" act="group_icon">');
                            newSrc.val(json.data.returnParams.fileSUrl);
                            elem.find("[act='group_icon_li']").show();
                        }
                    },
                    onUploadStart: function(file) {
                        $picUploadBtn.uploadify('settings', 'formData', {
                            'pageTenantId': SESSION.tenantId,
                            'uploadFileName': file.name,
                            'pageUserId': SESSION.user.id
                        });
                    }
                }
            });
        },
        _init:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            //var temp = require("oa/tmpl/group/content/tpl_group_set.tpl");
            //var tempUrl = 'oa/tmpl/group/content/tpl_group_create.tpl';
            var footer = me.getFooter();
            me.getBtn('ok').on('click', $.proxy(me.okClicked, me));
            footer.find('*[act="cancel"]').on('click', $.proxy(me.cancelClicked, me));
            elem.find(".ui-radio-buttonset").buttonset();
        },
        okClicked: function () {
            var me = this;
            var elem = me.element;
            me._doSave();//保存
        },
        cancelClicked: function () {
            var me = this;
            var elem = me.element;
            me.closeMe();//关闭
        },
        _doSave: function () {
            //setting
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var form = elem.find("[act='group_create_from']");
            var datas = form.serializeArray();
            var params = {};
            $.each(datas, function (i, json) {
                params[json.name] = json.value;
            });
            if(!params.groupName){
                rk.noticeError(rk.i18n('GROUP_PLEASE_INPUT_GROUP_NAME'));
                return;
            }
            me.getBtn('ok').beLoading();
            rk.post("/json/oa_group/add.action",params,function(json){
                if(json.status === 0){
                    //loaddataType
                    var panel = rk.oa.getPanel();
                    if(panel){
                        require.async(["../groupPanelCtrl"],function(){
                            panel.groupPanelCtrl("loaddata", opt.loaddataType);
                        });
                    }
                    me.closeMe();
                    rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                }else if(json.status === 217001){
                    me.getBtn('ok').beLoading('delay');
                    rk.noticeError(rk.i18n('GROUP_NO_PERMISSION'));
                }else{
                    me.getBtn('ok').beLoading('delay');
                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                }
            });
        }
    });
});